// utility/container.go
package utility

import (
	"reflect"
	"sync"
)

type Container struct {
	services map[reflect.Type]interface{}
	mu       sync.RWMutex
}

var globalContainer = &Container{
	services: make(map[reflect.Type]interface{}),
}

// Register 注册服务实例
func Register(serviceType reflect.Type, service interface{}) {
	globalContainer.mu.Lock()
	defer globalContainer.mu.Unlock()
	globalContainer.services[serviceType] = service

}

// Resolve 获取服务实例
func Resolve(serviceType reflect.Type) interface{} {
	globalContainer.mu.RLock()
	defer globalContainer.mu.RUnlock()

	return globalContainer.services[serviceType]
}

// MustResolve 获取服务实例(panic if not found)
func MustResolve(serviceType reflect.Type) interface{} {
	service := Resolve(serviceType)
	if service == nil {
		panic("service not registered: " + serviceType.String())
	}
	return service
}
